package com.ss.android.c;

import android.os.Handler;
import android.os.HandlerThread;

import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.baselib.network.http.AppConfig;
import com.bytedance.frameworks.baselib.network.http.AppConfig.d$c;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.newmedia.NewMediaConstants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class a implements d$c {
    public static AtomicBoolean a = new AtomicBoolean(false);
    private static a e;
    private final HandlerThread f;
    private final WeakHandler.Callback g;
    private final Handler h;
    public LinkedHashSet b;
    public ArrayList c;
    public volatile List d;

    private a() {
        this.b = new LinkedHashSet();
        this.c = new ArrayList();
        this.f = new HandlerThread("AppData-AsyncOp");
        this.f.start();
        this.g = new com.ss.android.c.b(this);
        this.h = new WeakHandler(this.f.getLooper(), this.g);
        AppConfig.a(this);
    }

    static LinkedHashSet a(a arg1) {
        return arg1.b;
    }

    public static a b() {
        if (e == null) {
            Class v1 = a.class;
            synchronized (v1) {
                if (e == null) {
                    e = new a();
                }
            }
        }
        return e;
    }

    private void a(LinkedHashSet arg5) {
        if (!arg5.isEmpty()) {
            LinkedHashSet v1 = this.b;
            synchronized (v1) {
                arg5.addAll(this.b);
            }

            int v2 = arg5.size() - 100;
            Iterator v3 = arg5.iterator();
            int v1_1;
            for (v1_1 = 0; v3.hasNext(); ++v1_1) {
                Object v0_1 = v3.next();
                if (v1_1 > v2) {
                    break;
                }

                arg5.remove(v0_1);
            }

            v1 = this.b;
            synchronized (v1) {
                this.b.addAll(arg5);
                return;
            }
        }
    }

    public final String a() {
        return "X-SS-Command";
    }

    public final void a(List arg11) {
        int v0_2;
        LinkedHashSet v4;
        JSONArray v3;
        Object v0;
        Iterator v2 = arg11.iterator();
        do {
            label_2:
            if (!v2.hasNext()) {
                return;
            }

            v0 = v2.next();
            if (v0 == null) {
                continue;
            }
            if (android.support.design.a.isTextEmpty(((String) v0))) {
                continue;
            }
            try {
                v3 = new JSONArray(((String) v0));
                v4 = new LinkedHashSet();
                v0_2 = 0;
                label_13:
                while (v0_2 < v3.length()) {
                    JSONObject v5 = v3.getJSONObject(v0_2);
                    long v6 = v5.optLong("i");
                    if (v6 > 0) {
                        v4.add(Long.valueOf(v6));
                        this.h.sendMessage(this.h.obtainMessage(104, v5));
                    }

                    ++v0_2;
                }

                if (v4.isEmpty()) {
                    continue;
                }

                this.a(v4);
                if (!a.compareAndSet(false, true)) {
                    continue;
                }
                new a$a(this).start();
            } catch (JSONException v0_1) {
                v0_1.printStackTrace();
            }
            continue;
        } while (true);
    }

    public interface b {
        void a(long arg1, String arg2, JSONObject arg3);
    }

    public static final class a$a extends Thread {
        private final a a;

        public a$a(a arg1) {
            this.a = arg1;
        }

        private boolean a() {
            Boolean v0_4;
            boolean v0_1;
            LinkedHashSet v2 = new LinkedHashSet();
            LinkedHashSet v3 = a.a(this.a);
            synchronized (v3) {
                v2.addAll(a.a(this.a));
            }

            if (v2.isEmpty()) {
                v0_1 = false;
                return v0_1;
            }

            StringBuilder v3_1 = new StringBuilder();
            Iterator v4 = v2.iterator();
            while (v4.hasNext()) {
                long v6 = ((Long) v4.next()).longValue();
                if (v6 <= 0) {
                    continue;
                }

                v3_1.append(v6).append(",");
            }

            if (v3_1.length() == 0) {
                return false;
            }

            String v0_2 = v3_1.substring(0, v3_1.length() - 1);
            try {
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    v0_4 = Boolean.valueOf(false);
                } else {
                    String v3_2 = NewMediaConstants.commandFeedbackUrl;
                    ArrayList v4_1 = new ArrayList();
                    v4_1.add(new NameValueImpl("command_ids", v0_2));
                    v0_2 = NetworkUtils.executePost(10240, v3_2, ((List) v4_1));
                    if (v0_2 != null && v0_2.length() != 0) {
                        v0_4 = Boolean.valueOf("success".equals(new JSONObject(v0_2).getString("message")));
//                        goto label_41;
                    } else {
                        v0_4 = Boolean.valueOf(false);
                    }
                }

//                label_41:
                v0_1 = v0_4.booleanValue();
            } catch (Exception v0_3) {
                v0_1 = false;
            }

            if (!v0_1) {
                return false;
            }

            LinkedHashSet v1 = a.a(this.a);
            synchronized (v1) {
                com.ss.android.c.a.a(this.a).removeAll(v2);
                return true;
            }
        }

        public final void run() {
            long v0 = AppLog.m();
            while (System.currentTimeMillis() - v0 < 60000) {
                if (!this.a()) {
                    break;
                }

                long v2 = 10000;
                try {
                    Thread.sleep(v2);
                } catch (InterruptedException v2_1) {
                    v2_1.printStackTrace();
                }
            }

            a.a.set(false);
        }
    }
}

